<template>
  <base-breadcrumb :breadcrumb="!isAudit">
    <div class="about-master-container">
      <b-steps
        :current="stepVal"
        direction="vertical"
        @change.self="stepChange"
      >
        <b-step title="基本信息">
          <a-card
            v-show="activeKey[0]"
            slot="description"
            v-loading="loading"
            :bordered="false"
          >
            <a-form-model
              ref="ruleForm"
              :model="formData"
              :rules="rules"
            >
              <a-row>
                <a-col span="8">
                  <a-form-model-item label="进场验收编码">
                    <BaseInput
                      v-model="formData.code"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="合同编号"
                    prop="materialContractCode"
                  >
                    <a-input-search
                      v-model="formData.materialContractCode"
                      placeholder="请选择"
                      :disabled="state === 3"
                      @search="openContract"
                      @click="openContract"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="合同名称">
                    <BaseInput
                      v-model="formData.materialContractName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col span="8">
                  <a-form-model-item label="工程名称">
                    <a-select
                      v-model="formData.constructionTypeId"
                      disabled
                    >
                      <a-select-option
                        v-for="item in programmeArr"
                        :key="item.id"
                        :value="item.id"
                      >
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="施工单位"
                    prop="projectId"
                  >
                    <SelectDownProject
                      v-model="formData.projectId"
                      :disabled="state === 3"
                      @selectChange="resetForm"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="生产厂家"
                    prop="manufacturer"
                  >
                    <BaseInput
                      v-model="formData.manufacturer"
                      :disabled="state === 3"
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col span="8">
                  <a-form-model-item label="产权单位">
                    <BaseInput
                      v-model="formData.supplier"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="安装单位"
                    prop="installationUnit"
                  >
                    <BaseInput
                      v-model="formData.installationUnit"
                      :disabled="state === 3"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="机械设备名称">
                    <BaseInput
                      v-model="formData.materialName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col span="8">
                  <a-form-model-item
                    label="机械设备编码"
                    prop="materialCode"
                  >
                    <a-input-search
                      v-model="formData.materialCode"
                      placeholder="请选择"
                      :disabled="state === 3"
                      @search="openMaterials"
                      @click="openMaterials"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="规格型号">
                    <BaseInput
                      v-model="formData.materialModel"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="单位">
                    <BaseInput
                      v-model="formData.materialUnit"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col span="8">
                  <a-form-model-item label="创建人">
                    <BaseInput
                      v-model="formData.creatorName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="创建时间">
                    <BaseInput
                      v-model="formData.createTime"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-form-model>
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                v-if="state !== 3"
                :loading="loading"
                type="primary"
                @click="temporaryStorage()"
              >
                保存草稿
              </a-button>
              <a-button
                :loading="loading"
                type="primary"
                @click="temporaryStorage(true)"
              >
                下一步
              </a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="验收明细">
          <a-card
            v-if="activeKey[1]"
            slot="description"
            :bordered="false"
          >
            <acceptance-detail
              :id="row ? row.id : null"
              :state="state"
            />
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                type="primary"
                @click="previous"
              >
                上一步
              </a-button>
              <a-button
                type="primary"
                :disabled="!row"
                @click="next"
              >
                下一步
              </a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="相关附件">
          <a-card
            v-if="activeKey[2]"
            slot="description"
            :bordered="false"
          >
            <u-table
              :upload-list="addFile"
              :delete-url="fileDel"
              :is-show="state !== 3"
              :list-url="fileList"
              :business-id="row.id"
              business-code="MM15"
            />
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                type="primary"
                @click="previous"
              >
                上一步
              </a-button>
            </div>
          </a-card>
        </b-step>
      </b-steps>
      <div
        v-if="!isAudit"
        class="pub-header-btn page-btn-right-top"
      >
        <a-button
          v-if="state !== 3"
          :loading="loading"
          type="primary"
          @click="submit"
        >
          提交
        </a-button>
        <BasePrintButton
          v-if="formData.id && state == 3"
          :before-print-fn="beforePrintFn"
          style="display: inline-block"
        />

        <a-button
          v-checkVisibleInDetail
          @click="$close"
        >
          关闭
        </a-button>
      </div>
      <contract-dialog
        ref="contractDialog"
        :other-params="{projectId:formData.projectId}"
        @getContract="getContract"
      />
      <material-list
        ref="materialsDialog"
        :url-fun="materialsDialogList"
        :params="{contractId:formData.contractId}"
        row-key="contractDetailsId"
        @createList="getMaterials"
      />
    </div>
      <selectApprover :businessId="businessId"  :processConfig="setProcessConfig"></selectApprover>
  </base-breadcrumb>
</template>
<script>
import contractDialog from '../components/contractDialog/contractDialog'
import acceptanceDetail from '../components/acceptanceDetail/acceptanceDetail'
import {
  deviceListAdd,
  deviceListEdit,
  audit,
  addFile,
  fileList,
  fileDel,
  deviceListDetail,
  print, materialsDialogList
} from '@/api/material/acceptance/device'
import { selectOption } from '@/api/material/goods/goods'
import { themeChoose } from '@/mixin/mixin'
import SelectDownProject from '@/components/Select/SelectDownProject'


export default {
  name: 'a' + Date.now(),
  mixins: [themeChoose],
  components: {
    contractDialog,
    acceptanceDetail,
    SelectDownProject
  },
  props: {
    isAudit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      materialsDialogList: materialsDialogList,
      beforePrintFn: async () => {
        let res = await print({ id: this.formData.id })
        return res.data.content
      },
      activeKey: [true, false, false, false, false, false, false],
      loading: false,
      stepVal: 0,
      state: Number(this.$route.query.state),
      isSubmit: false,
      row: this.$route.query.row,
      formData: {
        createTime: this.getNowTime(),
        creatorName: JSON.parse(sessionStorage.getItem('ycloud-user_info')).name,
        code: '',
        materialContractCode: '',
        materialContractName: '',
        constructionTypeId: null,
        manufacturer: '',
        supplier: '',
        installationUnit: '',
        materialName: '',
        materialCode: '',
        materialModel: '',
        materialUnit: '',
        projectId: '',
        contractId: null,
        materialId: '',
        supplierId: ''
      },
      rules: {
        materialContractCode: { required: true, message: '合同编号不能为空', trigger: 'change' },
        manufacturer: { required: true, message: '生产厂家不能为空', trigger: 'blur' },
        installationUnit: { required: true, message: '安装单位不能为空', trigger: 'blur' },
        materialCode: { required: true, message: '机械设备编码不能为空', trigger: 'change' },
        projectId: { required: true, message: '施工单位不能为空', trigger: 'change' }
      },
      isNext: false,
      addFile: addFile(),
      fileDel: fileDel(),
      fileList: fileList(),
      programmeArr: [],
      //获取流程配置值
      processConfig: null,
      //点击提交发送流程配置值
      setProcessConfig: null,
      businessId: null
    }
  },
  watch: {
    isAudit: {
      immediate: true,
      handler(val) {
        if (val) {
          this.state = 3
        }
      }
    },
    stepVal(newVal, oldVal) {
      this.$set(this.activeKey, newVal, true)
    }
  },
 async mounted() {
    this.getProgramme()
    if (this.state && this.state !== 1) {
      this.getEditInfo()
      this.isSubmit = true
    }
    //获取通过获取流程配置值
    this.processConfig = await this.$getConfigFn('mm_equipment_acceptance')
  },
  methods: {
    resetForm() {
      this.formData.materialContractCode = ''
      this.formData.materialContractName = ''
      this.formData.constructionTypeId = ''
      this.formData.supplier = ''
      this.formData.contractId = ''
      this.formData.supplierId = ''
    },
    previous: function() {
      this.isNext = true
      this.stepVal--
      if (this.stepVal < 0) {
        this.stepVal = 0
      }
    },
    openContract: function() {
      if (!this.formData.projectId) {
        this.$message.warning('请选选择施工单位')
        return false
      }
      this.$refs.contractDialog.open()
    },
    openMaterials: function() {
      if (!this.formData.contractId) {
        this.$message.warning('请先选择合同编号')
        return false
      }
      this.$refs.materialsDialog.open()
    },
    getMaterials: function(row) {
      this.formData.materialName = row.name
      this.formData.materialCode = row.code
      this.formData.materialModel = row.specificationModel
      this.formData.materialUnit = row.unit
      this.formData.materialId = row.materialId
    },
    getContract: function(row) {
      this.formData.materialContractCode = row.code
      this.formData.materialContractName = row.name
      this.formData.constructionTypeId = row.constructionTypeId
      this.formData.supplier = row.supplier
      this.formData.contractId = row.id
      this.formData.supplierId = row.supplierId
    },
    getProgramme: function() {
      let _this = this
      selectOption({
        type: 18
      }).then((res) => {
        if (res.code === 200) {
          _this.programmeArr = res.data
        }
      })
    },
    stepChange: function(val) {
      if (!this.row && !this.isAudit) {
        if (val) {
          this.$message.warning('切换步骤前请先填写基本信息')
          this.stepVal = 0
        }
      } else {
        if (!this.isNext) {
          this.$set(this.activeKey, val, !this.activeKey[val])
        }
      }
      this.isNext = false
    },
    // 获取编辑回显数据
    async getEditInfo() {
      const params = {
        id: this.$route.query.id || this.row.id
      }
      this.loading = true
      const res = await deviceListDetail(params)
      if (res.code === 200) {
        this.formData = this.row = res.data || {}
        this.formData.id = res.data.id
        this.formData.createTime = this.row.createTime
        this.formData.creatorName = this.row.creatorName
        this.formData.code = this.row.code
        this.formData.materialContractCode = this.row.materialContractCode
        this.formData.materialContractName = this.row.materialContractName
        this.formData.constructionTypeId = this.row.constructionTypeId
        this.formData.manufacturer = this.row.manufacturer
        this.formData.supplier = this.row.supplier
        this.formData.installationUnit = this.row.installationUnit
        this.formData.materialName = this.row.materialName
        this.formData.materialCode = this.row.materialCode
        this.formData.materialModel = this.row.materialModel
        this.formData.materialUnit = this.row.materialUnit
        this.formData.projectId = this.row.projectId
      }
      this.loading = false
    },
    submit() {
      // this.save('approve');
      this.setProcessConfig = null;
      if (this.isSubmit) {
        this.submitApprovalConfirm(this.processConfig,(flag) => {
          if(!flag) {
            this.loading = false
            return
          }
          this.loading = true
          audit({
            baseId: this.row.id,
            auditStatus: this.row.auditStatus
          }).then((res) => {
            this.$message.success('提交成功')
            this.loading = false
            //提交时，返回id
            this.businessId = this.row.id;
            this.setProcessConfig = this.processConfig;
          })
        })
      } else {
        this.$message.warning('提交之前请先暂存')
      }
    },
    temporaryStorage(isNext) {
      if (this.state === 3) {
        this.next()
        return
      }
      this.$refs.ruleForm.validate(async (valid, object) => {
        if (valid) {
          this.loading = true
          if (this.state === 2 || this.row) {
            this.formData.id = this.row.id
            let res = await deviceListEdit(this.formData)
            if (res.code === 200) {
              this.$message.success('暂存成功')
              this.isSubmit = true
              if (isNext) {
                this.next()
              }
            }
          } else {
            let res = await deviceListAdd(this.formData)
            if (res.code === 200) {
              this.row = res.data
              this.$message.success('暂存成功')
              this.isSubmit = true
              this.getEditInfo()
              if (isNext) {
                this.next()
              }
            }
          }
          this.loading = false
        } else {
          this.$message.warning("请填写完所有必填项信息")
          return false
        }
      })
    },
    next: function(val) {
      this.isNext = true
      this.stepVal++
    }
  }
}
</script>
